﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingsApp.Model;
using TrainingsApp.Logic.Import.Template;
using TrainingsApp.Logic.Import.Model;

namespace TrainingsApp.Logic.Import {
    public class SchoolTypeImport : ImportBase {
        ITemplateReader m_reader = new XLS_SchoolDataReader_1();

        public override bool Import(int keyId, string location, ImportType type) {
            Dictionary<int, object> dict = m_reader.ReadAll(keyId, location, 0/*ALL*/);
            foreach (var item in dict.Values) {
                List<SchoolTemplate_1> itemList = item as List<SchoolTemplate_1>;
                foreach (var model in itemList) {

                    ImportSchool(model);

                }
            }
            return true;
        }


        private void ImportSchool(SchoolTemplate_1 model) {

            TS_School_Entity entity = new TS_School_Entity();
            entity.Address = model.Address;
            entity.IsValid = 1;
            entity.ProvinceId = (int)this.GetProvinceId(model.ProvinceName);
            entity.AreaId = (int)this.GetAreaId(model.AreaName, entity.ProvinceId.Value);
            entity.RegionId = (int)this.GetRegionId(model.RegionName, entity.AreaId.Value);
            entity.TownId = (int)this.GetTownId(model.TownName, entity.RegionId.Value);
            entity.SchoolCode = model.SchoolCode;
            entity.SchoolName = model.SchoolName;
            entity.ZipCode = model.ZipCode;
            entity.SchoolType = (byte)EnumsHelper.ConvertSchoolType(model.SchoolType);

            TrainingsApp.Logic.TS_School_Logic.TS_School_Add(entity);
        }
    }
}
